<<<<<<< HEAD
package com.wyz.common;

import com.wyz.entity.Log;
import com.wyz.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Aspect
public class LogAspect {

    @Resource
    private LogService logService;

    @Around("@annotation(autoLog)")
    public Object doAround(ProceedingJoinPoint joinPoint, AutoLog autoLog) throws Throwable {
        // 操作内容，我们在注释里已经定义了 value 属性，然后在需要切入的接口面再去写上对应的操作内容
        String content = autoLog.value();

        // 操作时间，当前时间
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        // 操作人，默认为 "admin"
        String operator = "admin";

        // 操作人IP
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr();

        // 处理返回值，确保其类型正确
        Object result = joinPoint.proceed();

        if (result instanceof ResultVo<?>) {
            ResultVo<?> resultVo = (ResultVo<?>) result;
            Object data = resultVo.getData();

            // 进一步处理 data，例如如果是 AdminEntity，则更新 operator
           /* if (data instanceof AdminEntity) {
                AdminEntity admin = (AdminEntity) data;
                operator = admin.getUsername();
                System.out.println("用户名:" + operator);
            }*/
        } else {
            // 如果不是 ResultVo 类型，处理其他情况
//            System.out.println("返回值不是 ResultVo 类型");
        }

        // 插入数据到日志记录表
        Log log = new Log(content, time, operator, ip);
        logService.add(log);

        // 返回结果
        return result;
    }
}
=======
package com.wyz.common;

import com.wyz.entity.Log;
import com.wyz.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Aspect
public class LogAspect {

    @Resource
    private LogService logService;

    @Around("@annotation(autoLog)")
    public Object doAround(ProceedingJoinPoint joinPoint, AutoLog autoLog) throws Throwable {
        // 操作内容，我们在注释里已经定义了 value 属性，然后在需要切入的接口面再去写上对应的操作内容
        String content = autoLog.value();

        // 操作时间，当前时间
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        // 操作人，默认为 "admin"
        String operator = "admin";

        // 操作人IP
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr();

        // 处理返回值，确保其类型正确
        Object result = joinPoint.proceed();

        if (result instanceof ResultVo<?>) {
            ResultVo<?> resultVo = (ResultVo<?>) result;
            Object data = resultVo.getData();

            // 进一步处理 data，例如如果是 AdminEntity，则更新 operator
           /* if (data instanceof AdminEntity) {
                AdminEntity admin = (AdminEntity) data;
                operator = admin.getUsername();
                System.out.println("用户名:" + operator);
            }*/
        } else {
            // 如果不是 ResultVo 类型，处理其他情况
//            System.out.println("返回值不是 ResultVo 类型");
        }

        // 插入数据到日志记录表
        Log log = new Log(content, time, operator, ip);
        logService.add(log);

        // 返回结果
        return result;
    }
}
>>>>>>> be516e15a35375b38cf973b1ce88c92dd1ceba03
